<script>
export default {
  name: "sys_menu",
  mounted() {
    this.menuIds=[]
    this.selectMenuByCondition()

  },
  methods: {

    //普通删除
    deleteByIdMenu(row) {
      this.menuIds = []
      this.menuIds.push(row.menuId)
      console.log(this.menuIds)
      this.$http.post("menu/delete",this.menuIds).then((resp) => {
        if (resp.data.code == 200) {
          this.$message({
            showClose: true,
            message: '恭喜你，删除成功',
            type: 'success'
          });
          this.selectMenuByCondition()
        } else if (resp.data.code == 500) {
          this.$message({
            showClose: true,
            message: '删除失败',
            type: 'warning'
          });
        }
      })
      this.roleIds = []
    },
    //批量删除
    deleteByIdsMenu(done) {
      if (this.menuIds.length == 0) {
        this.$message({
          message: '请先选择权限',
          type: 'warning'
        });
        return

      }
      this.$confirm('是否批量删除？')
          .then(_ => {
            this.$http.post("menu/delete",this.menuIds).then((resp) => {
              if (resp.data.code == 200) {
                this.$message({
                  showClose: true,
                  message: '恭喜你，删除成功',
                  type: 'success'
                });
                this.selectMenuByCondition()
              }
            })
            this.roleIds = []
            done();
          })

          .catch(_ => {
          });
    },
    //多选
    handleSelectionChange(val) {
      console.log(val)
      this.menuIds=[]
      for (let i = 0; i < val.length; i++) {
        this.menuIds.push(val[i].menuId)
      }
      console.log(this.menuIds)
    },

    selectMenuByCondition() {
      this.$http.post("menu/select?current=" + this.currentPage + "&size=" + this.pageSize + "", this.menu).then((resp) => {
        this.tableData = resp.data.data.list;
        console.log(resp.data.data.list)
        this.total = resp.data.data.total;
      })
    },

    //筛选值
    searchSelect(data) {
      this.role.menus = data
      console.log(this.this.role.menus)
    },
    //关闭弹窗
    handleClose(done) {
      this.$confirm('确认关闭？')
          .then(_ => {
            this.menu = {}
            done();
          })
          .catch(_ => {
          });
    },
    updateMenu(row) {
      this.dialogVisible = true;
      console.log(row);
      this.menu = row
      this.isShow = false
    },
    //弹出框
    addMenu() {
      this.isShow = true
      this.dialogVisible = true;

    },
    addOrUpdateMenu() {

      if (this.menu.updateTime != "" && this.menu.updateTime != null) {
        console.log(this.menu)
        //修改


        this.$http.put("menu/update", this.menu).then((resp) => {
          if (resp.data.code == 200) {
            this.$message({
              showClose: true,
              message: '恭喜你，修改成功',
              type: 'success'
            });
            this.dialogVisible = false;
            this.menu = {}
            this.selectMenuByCondition()
          }
        }).catch((error) => {
          console.log(error)
        })

      } else {
        //添加
        this.$http.post("menu/add", this.menu).then((resp) => {
          if (resp.data.code == 200) {
            this.$message({
              showClose: true,
              message: '恭喜你，添加成功',
              type: 'success'
            });
          }
          this.dialogVisible = false;
          this.menu = {}
          this.selectMenuByCondition()
        }).catch((error) => {
          console.log(error)
        })
      }

    },
    //分页
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.selectMenuByCondition()
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val;
      this.selectMenuByCondition()
    },

  },
  data() {
    return {
      //多选
      multipleSelection: [],
      roleIds: [],
      options: [
        {
          value: "",
          label: '无'
        },
        {
          value: "启用",
          label: '启用'
        }, {
          value: "禁用",
          label: '禁用'
        }],
      label: "",
      menus: [],
      menuIds: [{}],
      dialogVisible: false,
      //分页
      currentPage: 1,
      pageSize: 5,
      total: 10,


      menu: {
        menuName: "",
        menuId: "",
        menuResource: "",
        perms: "",
        status: "",
        updateTime: ""
      },
      tableData: [{

      }]
    }
  }
}
</script>

<template>
  <div>
    <h1>我是权限管理</h1>
    <div>

      <el-input
          placeholder="请输入权限id"
          v-model="menu.menuId"
          style="width: 200px"
          clearable>
      </el-input>

      <el-input
          placeholder="请输入权限名称名称"
          v-model="menu.menuName"
          style="width: 200px;margin-left: 2px"
          clearable>
      </el-input>

      <el-select v-model="menu.status" placeholder="请选择"
                 style="margin-left: 2px;width: 100px;">
        <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
            style="width: 100px">
        </el-option>
      </el-select>
      <el-button type="primary" icon="el-icon-search" style="margin-left: 15px" @click="selectMenuByCondition">搜索
      </el-button>
      <el-button type="danger" round style="float: right;margin-right: 35px;position: relative"
                 @click="deleteByIdsMenu">批量删除
      </el-button>
      <el-button type="primary" round style="float: right;margin-right: 19px;position: relative" @click="addMenu">
        添加权限
      </el-button>
    </div>


    <el-table
        :data="tableData"
        border
        ref="multipleTable"
        style="width: 100%;margin-top: 1%"
        @selection-change="handleSelectionChange">
      <el-table-column
          type="selection"
          width="55">
      </el-table-column>
      <el-table-column
          prop="menuId"
          label="权限id"
          width="220">
      </el-table-column>
      <el-table-column
          prop="menuName"
          label="权限名称"
          width="180">
      </el-table-column>
      <el-table-column
          prop="menuResource"
          label="权限资源"
          width="200">
      </el-table-column>
      <el-table-column
          prop="perms"
          label="权限标识"
          width="180">
      </el-table-column>


      <el-table-column
          prop="status"
          label="状态"
          width="120">
      </el-table-column>


      <el-table-column
          fixed="right"
          label="操作"
          width="200">
        <template slot-scope="scope">
          <el-button @click="updateMenu(scope.row)" type="text" size="small">编辑</el-button>
          <el-button @click="deleteByIdMenu(scope.row)" type="text" size="small">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="5"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        style="margin-top: 2%"
    >
    </el-pagination>


    <!--添加form表单-->
    <el-dialog
        title="编辑角色"
        :visible.sync="dialogVisible"
        width="30%"
        :before-close="handleClose">
      <el-form ref="form" :model="menu" label-width="80px">
        <el-form-item label="权限名称">
          <el-input v-model="menu.menuName"></el-input>
        </el-form-item>
        <el-form-item label="权限资源">
          <el-input v-model="menu.menuResource"></el-input>
        </el-form-item>
        <el-form-item label="权限标识">
          <el-input v-model="menu.perms"></el-input>
        </el-form-item>

        <el-form-item label="状态">
          <el-tooltip placement="top">
            <el-switch
                v-model="menu.status"
                active-color="#13ce66"
                inactive-color="#ff4949"
                active-value="启用"
                inactive-value="禁用">
            </el-switch>
          </el-tooltip>
        </el-form-item>

      </el-form>
      <el-button @click="dialogVisible = false">取 消</el-button>
      <el-button type="primary" @click="addOrUpdateMenu">确 定</el-button>

    </el-dialog>


  </div>
</template>

<style scoped>
.el-pagination {
  text-align: center;
}
</style>